library MultiboardSystem initializer init

	function MBSet takes multiboard mb, integer col, integer row, string val returns nothing
		local multiboarditem mbitem = MultiboardGetItem(mb, row - 1, col - 1)
		call MultiboardSetItemValue(mbitem, val)
		call MultiboardReleaseItem(mbitem)
		set mbitem = null
	endfunction

	function MBIcon2 takes multiboard mb, integer col, integer row, string iconFileName returns nothing
		local multiboarditem mbitem = MultiboardGetItem(mb, row - 1, col - 1)
		call MultiboardSetItemIcon(mbitem, iconFileName)
		call MultiboardReleaseItem(mbitem)
		set mbitem = null
	endfunction


	function MBColor takes integer col, integer row, integer red, integer green, integer blue, integer visibility returns nothing
		local multiboarditem mbitem = MultiboardGetItem(udg_SquadStatus, row, col)
		call MultiboardSetItemValueColor(mbitem, red, green, blue, visibility)
		call MultiboardReleaseItem(mbitem)
		set mbitem = null
	endfunction

	private function PeriodicUpdates takes nothing returns nothing
		local integer i = 1
		local integer i2
		local integer i3
		local real r = 0.0
		set udg_Second = udg_Second + 1
		if udg_Second>=60 then
			set udg_Second = 0
			set udg_Minute = udg_Minute + 1
		endif
		if udg_Minute>=60 then
			set udg_Minute = 0
			set udg_Hour = udg_Hour + 1
		endif
		call MBSet( udg_SquadStatus,1,2, "Mission Time : " + udg_Clock[udg_Hour] + ":" + udg_Clock[udg_Minute] + ":" + udg_Clock[udg_Second] )
		loop
			exitwhen i>udg_InitialPlayers
			set i2 = udg_MBcolors[udg_Sorted[i]]
			if i2==0 then //Dead (Red)
				call MBColor(0, i+2, 138, 82, 82, 64) //Range 0-255
			elseif i2==1 then //CO (DarkOrange)
				call MBColor(0, i+2, 204, 140, 46, 128)
			elseif i2==2 then //NCO (Khaki)
				call MBColor(0, i+2, 140, 135, 79, 128)
			elseif i2==3 then //Leaver (Gray)
				call MBColor(0, i+2, 128, 128, 128, 64)
			else //i2==4 //Enlisted (DarkOliveGreen)
				call MBColor(0, i+2, 84, 120, 46, 128)
			endif 
			call MBIcon2(udg_SquadStatus,1,i+3,udg_PlayerIcon[udg_Sorted[i]])
			call MBSet( udg_SquadStatus,1,i+3,GetPlayerName(ConvertedPlayer(udg_Sorted[i])))
			if udg_Hero[udg_Sorted[i]]==null then
				set r = 0.0
			else
				set r = GetWidgetLife(udg_Hero[udg_Sorted[i]]) / GetUnitState(udg_Hero[udg_Sorted[i]],UNIT_STATE_MAX_LIFE) * 100.0
			endif
			call MBSet( udg_SquadStatus,2,i+3,I2S(R2I(r)) + "%")
			if udg_SITREP==1 then //Experience
				call MBSet( udg_SquadStatus,3,i+3,I2S(R2I(udg_Experience[udg_Sorted[i]])))
			elseif udg_SITREP==2 then //Kills
				call MBSet( udg_SquadStatus,3,i+3,I2S(GetPlayerScore(ConvertedPlayer(udg_Sorted[i]),PLAYER_SCORE_UNITS_KILLED)))
			else //ms
				call MBSet( udg_SquadStatus,3,i+3,I2S(R2I(GetUnitMoveSpeed(udg_Hero[udg_Sorted[i]]))))
			endif
			set i = i + 1
		endloop
		set i2 = 0
		loop
			exitwhen i2==1
			set i2 = 1
			set i = 1
			loop
				exitwhen i >= udg_Players
				if udg_Experience[udg_Sorted[i]]<udg_Experience[udg_Sorted[i+1]] then
					set i3 = udg_Sorted[i]
					set udg_Sorted[i] = udg_Sorted[i+1]
					set udg_Sorted[i+1] = i3
					set i2 = 0
				endif
				set i = i + 1
			endloop
		endloop
	endfunction

	private function init takes nothing returns nothing
		local trigger t= CreateTrigger()
		call TriggerAddAction(t,function PeriodicUpdates)
	    call TriggerRegisterTimerEvent(t, 1.0, true)
	endfunction
	
endlibrary
